import { _decorator, Component, instantiate, Node, Prefab } from 'cc';
import { GameWeapon } from './GameWeapon';
import { Gun } from './Gun';
import PlayerController from '../PlayerController';
import { EventManager, MyEvent } from '../AshFramework/Managers/EventManager';
import { ResourceUtil } from '../AshFramework/Utils/ResourceUtil';
import { GameManager } from './GameManager';
import { HintController } from './HintController';
import { BULLETNAME, CAPACITY, WEAPON, WEAPONBULLET } from '../AshFramework/Datas/Constant';
import { ZTool } from '../AshFramework/Utils/ZTool';
import { Magazine } from './Magazine';
import { Reload } from './Loading';
import { BundleManager, Bundles } from '../AshFramework/Managers/BundleManager';
import { BulletController_LDP } from './BulletController_LDP';
import { TLWLSJ_UIManager } from './UIManager';
const { ccclass, property } = _decorator;

@ccclass('GameWeapon_DCP')
export class GameWeapon_DCP extends GameWeapon {

    @property
    CoolingTime: number = 1;

    Muzzle: Node = null;

    private _data: Gun = null;
    private _capacity: number = 0;

    protected start(): void {
        this.Muzzle = this.node.getChildByName("枪口");
        this._data = this.Data as Gun;
        PlayerController.Instance.WeaponTs = this;
        this._capacity = CAPACITY[ZTool.GetEnumKeyByValue(WEAPON, this.WeaponType)];
        if (!this._data.CurMagazine) {
            this._data.CurMagazine = new Magazine(this._capacity);
        }
        this.scheduleOnce(() => { this.updateWeaponShow(this._data.CurMagazine); }, 0.1);
    }

    //却换弹夹
    loadBullet() {
        if (TLWLSJ_UIManager.Instance.IsLock) {
            this.getWeapon();
            return;
        }
        if (this._data.CurMagazine.Bullets.length >= this._capacity) {
            ResourceUtil.LoadPrefab("UI/Hint").then((prefab: Prefab) => {
                const node: Node = instantiate(prefab);
                node.parent = GameManager.Instance.Canvas;
                node.getComponent(HintController).showHint("弹夹已满！");
            })
            return;
        }


        if (!GameManager.Instance.IsRecharge) {
            TLWLSJ_UIManager.Instance.showCDZPanel(() => {
                TLWLSJ_UIManager.Instance.closeCDZPanel();
                const weaponName = ZTool.GetEnumKeyByValue(WEAPON, this.WeaponType);
                this._data.CurMagazine = new Magazine(CAPACITY[weaponName], WEAPONBULLET[weaponName]);
                this.updateWeaponShow(this._data.CurMagazine);
            });
            return;
        }

        this.IsReload = true;
        const needNum = this._capacity - this._data.CurMagazine.Bullets.length;
        for (let i = 0; i < needNum; i++) {
            this._data.CurMagazine.Bullets.push(BULLETNAME.DCP);
        }
        BundleManager.LoadPrefab(Bundles.Prefabs, "装弹中").then((prefab: Prefab) => {
            const reload: Node = instantiate(prefab);
            reload.parent = GameManager.Instance.Canvas;
            reload.getComponent(Reload).reload(() => {
                this.IsReload = false;
                this.updateWeaponShow(this._data.CurMagazine);
            })
        })
    }

    attack() {
        if (TLWLSJ_UIManager.Instance.IsLock) {
            this.getWeapon();
            return;
        }
        if (!this.isAttack()) return;
        if (this.IsStop) {
            return;
        }

        if (this._data.CurMagazine && this._data.CurMagazine.Bullets.length > 0) {
            this.IsFire = true;
            const bullet = this._data.CurMagazine.Bullets.shift();
            PlayerController.Instance.applyRecoil(100000);
            this.scheduleOnce(() => {
                PlayerController.Instance.IsRecoil = false;
            }, 0.2);
            this.fireBullet(bullet);
            this.attackAni(() => {
                this.IsFire = false;
                this.updateWeaponShow(this._data.CurMagazine);
                this.attack();
            })
        } else {
            this.loadBullet();
            this.endAttack();
        }
    }

    attackAni(cb: Function = null) {
        this.scheduleOnce(() => {
            cb && cb();
        }, this.CoolingTime);
    }

    //发射子弹
    fireBullet(bulletName: string) {
        BundleManager.LoadPrefab(Bundles.Prefabs, "子弹_电磁炮").then((prefab: Prefab) => {
            const bullet: Node = instantiate(prefab);
            bullet.parent = GameManager.Instance.BulletLayout;
            bullet.setWorldPosition(this.Muzzle.getWorldPosition());
            bullet.getComponent(BulletController_LDP).fire(bulletName, PlayerController.Instance.DirX, PlayerController.Instance.DirY);
        });
    }

    protected onEnable(): void {
        super.onEnable();
        EventManager.on(MyEvent.RELOAD, this.loadBullet, this);
    }

    protected onDisable(): void {
        super.onDisable();
        EventManager.off(MyEvent.RELOAD, this.loadBullet, this);
    }
}


